﻿/**
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thorqq.magictimer;

//import com.android.deskclock2.R;

import com.baidu.mobstat.StatService;
import com.thorqq.magictimer.util.Util;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;

/***
 * Settings for the Alarm Clock. 
 * 1、静音模式下闹铃是否响 
 * 2、闹铃音量 
 * 3、延时提醒间隔 
 * 4、侧边按钮功能 
 * 5、**背景图片
 * 6、闹铃持续时间
 * 7、调测模式
 * 
 * -3、**帮助 
 * -2、**意见反馈 
 * -1、**版权所有
 */
public class ConfigurationActivity extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener
{
    // 静音模式下闹铃是否响
    private static final String KEY_ALARM_IN_SILENT_MODE = "alarm_in_silent_mode";
    private static final int ALARM_STREAM_TYPE_BIT = 1 << AudioManager.STREAM_ALARM;
    // 延时提醒时间
    public static final String KEY_ALARM_SNOOZE = "snooze_duration";
    public static final String DEFAULT_SNOOZE_MINUTE = "5";
    // 侧边按钮功能
    public static final String KEY_VOLUME_BEHAVIOR = "volume_button_setting";
    public static final String DEFAULT_VOLUME_BEHAVIOR = "2";
    // 闹铃持续时间
    public static final String KEY_ALARM_TIMEOUT_SECONDS = "alarm_timeout_seconds";
    public static final String DEFAULT_ALARM_TIMEOUT_SECONDS = "30";
    // 调测开关
    public static final String KEY_DEBUG_FLAG = "debug_flag";
    public static final boolean DEFAULT_DEBUG_FLAG = true;
    // 更新检查开关
    public static final String KEY_CHECK_UPDATE_FLAG = "check_update_flag";
    public static final boolean DEFAULT_CHECK_UPDATE_FLAG = true;
    // 版本名
    private static final String KEY_SOFTWARE_NAME = "software_name";
    // E-mail
    private static final String KEY_EMAIL = "email";
    // Homepage
//    private static final String KEY_HOMEPAGE = "homepage";


    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
       
     }

    @Override
    protected void onResume()
    {
        super.onResume();
        StatService.onResume(this);
        refresh();
    }

    @Override
    public void onPause()
    {
        super.onPause();

        /**
         * 此处调用基本统计代码
         */
        StatService.onPause(this);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
    {
        if (KEY_ALARM_IN_SILENT_MODE.equals(preference.getKey()))
        {
            CheckBoxPreference pref = (CheckBoxPreference) preference;
            int ringerModeStreamTypes = Settings.System.getInt(getContentResolver(),
                    Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);

            if (pref.isChecked())
            {
                ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT;
            } else
            {
                ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT;
            }

            Settings.System.putInt(getContentResolver(),
                    Settings.System.MODE_RINGER_STREAMS_AFFECTED, ringerModeStreamTypes);

            return true;
        }
        else if(KEY_DEBUG_FLAG.equals(preference.getKey()))
        {
            CheckBoxPreference pref = (CheckBoxPreference) preference;
            Util.setDebug(pref.isChecked());
        }
        else if(KEY_CHECK_UPDATE_FLAG.equals(preference.getKey()))
        {
            CheckBoxPreference pref = (CheckBoxPreference) preference;
            Configuration.getIntance().setCheckUpdate(pref.isChecked());
        }

        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    public boolean onPreferenceChange(Preference pref, Object newValue)
    {
        final ListPreference listPref = (ListPreference) pref;
        final int idx = listPref.findIndexOfValue((String) newValue);
        listPref.setSummary(listPref.getEntries()[idx]);
        return true;
    }

    private void refresh()
    {
        //静音模式下闹铃是否响
        final CheckBoxPreference alarmInSilentModePref = (CheckBoxPreference) findPreference(KEY_ALARM_IN_SILENT_MODE);
        final int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
        alarmInSilentModePref.setChecked((silentModeStreams & ALARM_STREAM_TYPE_BIT) == 0);

        //延时提醒时间
        final ListPreference snooze = (ListPreference) findPreference(KEY_ALARM_SNOOZE);
        snooze.setSummary(snooze.getEntry());
        snooze.setOnPreferenceChangeListener(this);

        //闹铃持续时间
        final ListPreference alarm_timeout = (ListPreference) findPreference(KEY_ALARM_TIMEOUT_SECONDS);
        alarm_timeout.setSummary(alarm_timeout.getEntry());
        alarm_timeout.setOnPreferenceChangeListener(this);
        
        //调测开关
        final CheckBoxPreference debugFlag = (CheckBoxPreference) findPreference(KEY_DEBUG_FLAG);
        debugFlag.setChecked(Util.is_debug());
        
        //检查更新开关
        final CheckBoxPreference checkUpdateFlag = (CheckBoxPreference) findPreference(KEY_CHECK_UPDATE_FLAG);
        checkUpdateFlag.setChecked(Configuration.getIntance().isCheckUpdate());

        //版本名
        final Preference softwareName = (Preference) findPreference(KEY_SOFTWARE_NAME);
        softwareName.setTitle(ConfigurationActivity.this.getString(R.string.app_name));
        softwareName.setSummary("当前版本" + 
                StatHelper.getInstance().getValue(StatHelper.APP_VERSION_NAME)
                + "，点击查询最新版本");
        softwareName.setOnPreferenceClickListener(new OnPreferenceClickListener()
        {
            @Override
            public boolean onPreferenceClick(Preference arg0)
            {                
                UmengUpdateManager.getIntance().checkUpdate(ConfigurationActivity.this);
                return true;
            }
            
        });
        
        //意见反馈
        final Preference email = (Preference) findPreference(KEY_EMAIL);
        email.setOnPreferenceClickListener(new OnPreferenceClickListener()
        {
            @Override
            public boolean onPreferenceClick(Preference arg0)
            {
                String deviceName = StatHelper.getInstance().getValue(StatHelper.DEVICE_NAME);
                String versionName = StatHelper.getInstance().getValue(StatHelper.APP_VERSION_NAME);
                
                Intent data=new Intent(Intent.ACTION_SENDTO);  
                data.setData(Uri.parse("mailto:" + ConfigurationActivity.this.getString(R.string.email)));  
                data.putExtra(Intent.EXTRA_SUBJECT, "意见反馈（机型" + deviceName + "，版本" + versionName + "）");  
                startActivity(data);  
                return true;
            }
            
        });
        

    }

}
